package com.ls.fw.office.itext.pdf.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class PdfUtil {

	
	/**
	 * 转换为pdf
	 * @author Defender 
	 * 2014年5月23日下午2:15:43
	 * @param in
	 * @param pdfOut
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void toPdf(InputStream in,OutputStream pdfOut) throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, pdfOut);
        document.open();
        try {
			XMLWorkerHelper.getInstance().parseXHtml(writer, document, in);
		} catch (IOException e) {
			throw e;
		}finally{
			 document.close();
		     writer.flush();
		     writer.close();
		}
	}
	
	
	/**
	 * 转换为pdf
	 * @author Defender 
	 * 2014年5月23日下午2:16:01
	 * @param in
	 * @param path
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void toPdf(InputStream in,String path) throws DocumentException, IOException{
		toPdf(in,new FileOutputStream(path));
	}
	
	public static void toPdf(Reader in,OutputStream pdfOut) throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, pdfOut);
        document.open();
        try {
			XMLWorkerHelper.getInstance().parseXHtml(writer, document,in);
		} catch (IOException e) {
			throw e;
		}finally{
	        document.close();
	        writer.flush();
	        writer.close();
		}
	}
	
	
	/**
	 * 转换为pdf
	 * @author Defender 
	 * 2014年5月23日下午2:16:01
	 * @param in
	 * @param path
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void toPdf(Reader in,String path) throws DocumentException, IOException{
		toPdf(in,new FileOutputStream(path));
	}
}
